package net.FamilyTree.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.FamilyTree.entity.SystemAdmin;
import net.FamilyTree.form.RuleForm;
import net.FamilyTree.mapper.SystemAdminMapper;
import net.FamilyTree.service.SystemAdminService;
import net.FamilyTree.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-05-22
 */
@Service
public class SystemAdminServiceImpl extends ServiceImpl<SystemAdminMapper, SystemAdmin> implements SystemAdminService {
    @Autowired
    private SystemAdminMapper systemAdminMapper;
    /**
     * 实现登录功能
     * 用户名不存在返回-1
     * 密码错误返回-2
     * 用户名和密码都正确返回0
     * @param ruleForm
     * @return
     */
    @Override
    public ResultVo login(RuleForm ruleForm) {
        //1、判断用户名是否存在
        LambdaQueryWrapper<SystemAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemAdmin::getUsername, ruleForm.getUsername());
        SystemAdmin systemAdmin = this.systemAdminMapper.selectOne(queryWrapper);
        ResultVo resultVO = new ResultVo<>();
        if(systemAdmin== null){
            resultVO.setCode(-1);
        } else {
            //2、判断密码是否正确
            if(!systemAdmin.getPassword().equals(ruleForm.getPassword())){
                resultVO.setCode(-2);
            } else {
                resultVO.setCode(0);
                resultVO.setData(systemAdmin);
            }
        }
        return resultVO;
    }
}
